#include "GameOverScene.h"
#include <graphics.h>
#include <iostream>
#include <tchar.h>
#include <string>

GameOverScene::GameOverScene(const std::string& name)
    : Scene(name), finalScore_(0), gameWon_(false), timeElapsed_(0.0f), animationTime_(0.0f),
    rKeyPressed_(false), escKeyPressed_(false), mKeyPressed_(false) {
    inputHandler_ = std::make_unique<InputHandler>();
}

GameOverScene::~GameOverScene() = default;

void GameOverScene::OnCreate() {
    std::cout << "Game Over scene created" << std::endl;
}

void GameOverScene::OnDestroy() {
    std::cout << "Game Over scene destroyed" << std::endl;
}

void GameOverScene::OnActivate() {
    std::cout << "Game Over scene activated" << std::endl;
    animationTime_ = 0.0f;

    // Reset input states to prevent immediate triggering
    rKeyPressed_ = (GetAsyncKeyState('R') & 0x8000) != 0;
    escKeyPressed_ = (GetAsyncKeyState(VK_ESCAPE) & 0x8000) != 0;
    mKeyPressed_ = (GetAsyncKeyState('M') & 0x8000) != 0;
}

void GameOverScene::OnDeactivate() {
    std::cout << "Game Over scene deactivated" << std::endl;
}

void GameOverScene::Update(float deltaTime) {
    animationTime_ += deltaTime;
    checkInput();
}

void GameOverScene::Render() {
    // Set background color based on win/lose
    if (gameWon_) {
        setbkcolor(RGB(50, 150, 50)); // Green for victory
    }
    else {
        setbkcolor(RGB(150, 50, 50)); // Red for defeat
    }
    cleardevice();

    renderGameOverTitle();
    renderResults();
    renderOptions();
}

void GameOverScene::checkInput() {
    // Get current key states
    bool rCurrentlyPressed = (GetAsyncKeyState('R') & 0x8000) != 0;
    bool escCurrentlyPressed = (GetAsyncKeyState(VK_ESCAPE) & 0x8000) != 0;
    bool mCurrentlyPressed = (GetAsyncKeyState('M') & 0x8000) != 0;

    // Check for key press (not held)
    if (rCurrentlyPressed && !rKeyPressed_) {
        std::cout << "Restart game pressed" << std::endl;
        if (m_sceneManager) {
            m_sceneManager->SwitchToScene("PlatformJump");
        }
    }

    if ((escCurrentlyPressed && !escKeyPressed_) || (mCurrentlyPressed && !mKeyPressed_)) {
        std::cout << "Return to menu pressed" << std::endl;
        if (m_sceneManager) {
            m_sceneManager->SwitchToScene("Menu");
        }
    }

    // Update previous key states
    rKeyPressed_ = rCurrentlyPressed;
    escKeyPressed_ = escCurrentlyPressed;
    mKeyPressed_ = mCurrentlyPressed;
}

void GameOverScene::renderGameOverTitle() {
    settextcolor(RGB(255, 255, 255));
    setbkmode(TRANSPARENT);

    int windowWidth = 800;
    int windowHeight = 600;
    int titleY = windowHeight / 6;

    // Title with shadow
    settextstyle(48, 0, _T("Arial"));

    TCHAR title[20];
    if (gameWon_) {
        _tcscpy_s(title, _T("VICTORY!"));
    }
    else {
        _tcscpy_s(title, _T("GAME OVER"));
    }

    // Shadow
    settextcolor(RGB(0, 0, 0));
    int titleWidth = textwidth(title);
    int titleX = (windowWidth - titleWidth) / 2;
    outtextxy(titleX + 2, titleY + 2, title);

    // Main title
    if (gameWon_) {
        settextcolor(RGB(255, 255, 100)); // Gold for victory
    }
    else {
        settextcolor(RGB(255, 100, 100)); // Light red for defeat
    }
    outtextxy(titleX, titleY, title);
}

void GameOverScene::renderResults() {
    settextcolor(RGB(255, 255, 255));
    settextstyle(32, 0, _T("Arial"));

    int windowWidth = 800;
    int windowHeight = 600;
    int resultY = windowHeight / 2 - 50;

    // Score display
    char scoreText[50];
    sprintf_s(scoreText, "Final Score: %d", finalScore_);
    TCHAR wScoreText[50];
    MultiByteToWideChar(CP_ACP, 0, scoreText, -1, wScoreText, 50);

    int scoreWidth = textwidth(wScoreText);
    outtextxy((windowWidth - scoreWidth) / 2, resultY, wScoreText);

    // Time display
    char timeText[50];
    sprintf_s(timeText, "Time Survived: %.1fs", timeElapsed_);
    TCHAR wTimeText[50];
    MultiByteToWideChar(CP_ACP, 0, timeText, -1, wTimeText, 50);

    int timeWidth = textwidth(wTimeText);
    outtextxy((windowWidth - timeWidth) / 2, resultY + 40, wTimeText);
    // Win/Lose message
    settextstyle(24, 0, _T("Arial"));
    TCHAR message[100];
    if (gameWon_) {
        _tcscpy_s(message, _T("Congratulations! You survived 20 seconds!"));
        settextcolor(RGB(100, 255, 100));
    }
    else {
        _tcscpy_s(message, _T("Try again! You can do better!"));
        settextcolor(RGB(255, 150, 150));
    }

    int messageWidth = textwidth(message);
    outtextxy((windowWidth - messageWidth) / 2, resultY + 80, message);
}

void GameOverScene::renderOptions() {
    settextcolor(RGB(255, 255, 255));
    settextstyle(24, 0, _T("Arial"));

    int windowWidth = 800;
    int windowHeight = 600;
    int optionsY = windowHeight * 3 / 4;

    // Options with pulsating effect
    float pulseIntensity = (sin(animationTime_ * 3.0f) + 1.0f) * 0.5f;
    int pulseColor = (int)(100 + pulseIntensity * 155);

    TCHAR option1[] = _T("Press R to restart");
    TCHAR option2[] = _T("Press M or ESC to return to menu");

    settextcolor(RGB(pulseColor, pulseColor, 255));
    int option1Width = textwidth(option1);
    outtextxy((windowWidth - option1Width) / 2, optionsY, option1);

    settextcolor(RGB(255, pulseColor, pulseColor));
    int option2Width = textwidth(option2);
    outtextxy((windowWidth - option2Width) / 2, optionsY + 30, option2);
}
